package com.fanqwz.hexieshe.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;

import com.fanqwz.hexieshe.Navigator;
import com.fanqwz.hexieshe.R;
import com.fanqwz.hexieshe.activity.BaseActivity;
import com.fanqwz.hexieshe.adapter.HeadLineAdapter;
import com.fanqwz.hexieshe.model.HeadLineModel;
import com.fanqwz.hexieshe.presenter.HeadLinePresenter;
import com.fanqwz.hexieshe.ui.SwipeRefreshLoadLayout;
import com.fanqwz.hexieshe.view.HeadlineView;

import java.util.List;

import javax.inject.Inject;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by fanqw on 2017/7/1.
 */

public class HeadLineFragment extends BaseFragment implements HeadlineView, SwipeRefreshLayout.OnRefreshListener, SwipeRefreshLoadLayout.OnLoadLisener {

    @Bind(R.id.recyclerview)
    RecyclerView recyclerView;
    @Bind(R.id.refreshlayout)
    SwipeRefreshLoadLayout swipeRefreshLoadLayout;

    @Inject
    HeadLinePresenter presenter;
    @Inject
    HeadLineAdapter headLineAdapter;
    @Inject
    Navigator navigator;
    @Inject
    ActionBar actionBar;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ((BaseActivity) getActivity()).getComponent().inject(this);
//        ((Application) getActivity().getApplication()).getComponent().inject(this);
        presenter.setView(this);
        presenter.create();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.headline_fragment, container, false);
        ButterKnife.bind(this, root);
        swipeRefreshLoadLayout.setOnRefreshListener(this);
        swipeRefreshLoadLayout.setOnLoadListener(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(container.getContext()));
        recyclerView.setAdapter(headLineAdapter);
        headLineAdapter.setOnClickListener(view -> {
            int position = recyclerView.getChildAdapterPosition(view);
            HeadLineModel headLine = headLineAdapter.getHeadlines().get(position);
            navigator.showArticle(headLine.getId());
        });
        presenter.show();
        return root;
    }

    @Override
    public void onResume() {
        super.onResume();
        Bundle bundle = getArguments();

        if (bundle != null) {
            presenter.setCategory(bundle.getString("category"));
            presenter.setTag(bundle.getString("tag"));
        }


    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onDestroyView() {
        presenter.hide();
        ButterKnife.unbind(this);
        super.onDestroyView();
    }

    @Override
    public void onDestroy() {
        presenter.destroy();
        super.onDestroy();
    }

    @Override
    public void prepare() {
        recyclerView.setVisibility(View.GONE);
        swipeRefreshLoadLayout.setRefreshing(true);
    }

    @Override
    public void setTitle(String title) {
        actionBar.setTitle(title);
    }

    @Override
    public void showHeadlines(List<HeadLineModel> articles) {
        swipeRefreshLoadLayout.setRefreshing(false);
        swipeRefreshLoadLayout.setLoaded();
        recyclerView.setVisibility(View.VISIBLE);

        if (articles != null) {
            headLineAdapter.setHeadlines(articles);
            headLineAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onRefresh() {
        presenter.refresh();
    }

    @Override
    public void onLoad() {
        presenter.getMore();
    }
}
